//
//  XMMyMeetingDraftCell.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/26.
//

#import "XMMyMeetingDraftCell.h"

@interface XMMyMeetingDraftCell ()

@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *stateLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic, strong) UIButton *editBtn;

@end

@implementation XMMyMeetingDraftCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.avatarImgView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.stateLabel];
    
    UILabel *dateTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), _avatarImgView.maxY + KScaleWidth(15.0), kScreenWidth/2 - KScaleWidth(16.0)*2, KScaleWidth(27.0)) font:KFontSize(12.0) textColor:kColorHEX(0x666666)];
    dateTitleLabel.text = @"保存时间";
    [self addSubview:dateTitleLabel];
    [self addSubview:self.dateLabel];
    [self.contentView addSubview:self.deleteBtn];
    [self.contentView addSubview:self.editBtn];
}

- (void)setContentWithOrderModel:(XMMeetingModel *)orderModel
                          isSelf:(BOOL)isSelf {
    _deleteBtn.hidden = !isSelf;
    _editBtn.hidden = !isSelf;
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:orderModel.coverImageUrl] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    CGFloat height = [orderModel.title getHeightWithFont:KScaleWidth(16.0) withWidth:_nameLabel.width] + KScaleWidth(10.0);
    _nameLabel.height = MIN(height, KScaleWidth(54.0));
    _nameLabel.text = orderModel.title;//@"摩托估值选车指导交流这是产品名称这是产品名称";
    NSTimeInterval timInterval = [orderModel.time integerValue]/1000;
    _dateLabel.text = [[NSDate dateWithTimeIntervalSince1970:timInterval] dateStringWithFormat:@"yyyy-MM-dd HH:mm"];
}

//编辑
- (void)editAction:(UIButton *)button {
    self.meetingEditBlock ? self.meetingEditBlock() : nil;
}

//删除
- (void)deleteAction:(UIButton *)button {
    self.meetingDeleteBlock ? self.meetingDeleteBlock() : nil;
}

#pragma mark - Init
- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(80.0), KScaleWidth(80.0))];
        [_avatarImgView setCornerRadius:KScaleWidth(8.0)];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(102.0), KScaleWidth(18.0), kScreenWidth - KScaleWidth(102.0) - KScaleWidth(16.0), KScaleWidth(54.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

- (UILabel *)stateLabel {
    if (!_stateLabel) {
        _stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.x, self.avatarImgView.maxY - KScaleWidth(23.0) - KScaleWidth(5.0), _nameLabel.width, KScaleWidth(23.0)) font:KFontSize(12.0) textColor:kColorHEX(0xDD4B39)];
        _stateLabel.backgroundColor = kColorHEX(0xFCEDED);
        [_stateLabel setCornerRadius:KScaleWidth(3.0)];
        _stateLabel.text = @"  宝贝可以直接发布啦";
    }
    return _stateLabel;
}

- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth/2, self.stateLabel.maxY + KScaleWidth(15.0), kScreenWidth/2 - KScaleWidth(16.0), KScaleWidth(27.0)) font:KFontSize(12.0) textColor:kColorHEX(0x666666) textAlignment:NSTextAlignmentRight];
    }
    return _dateLabel;
}

- (UIButton *)editBtn {
    if (!_editBtn) {
        _editBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(kScreenWidth - KScaleWidth(16.0) - KScaleWidth(90.0), self.dateLabel.maxY + KScaleWidth(10.0), KScaleWidth(90.0), KScaleWidth(38.0)) title:@"编辑" titleColor:kColorHEX(0x333333)];
        [_editBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
        [_editBtn setCornerRadius:_editBtn.height/2];
        [_editBtn addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editBtn;
}

- (UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(14.0) frame:CGRectMake(kScreenWidth - KScaleWidth(115.0) - KScaleWidth(89.0), self.editBtn.y, KScaleWidth(89.0), KScaleWidth(38.0)) title:@"删除" titleColor:kColorHEX(0x333333)];
        [_deleteBtn setRadius:_deleteBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
        [_deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
